#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import wx.gizmos   as  gizmos
import os

ID_SAVE    = 101
ID_RESTORE = 102
ID_EXIT    = 103

class SysPathEditor(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, title="System path editor")
        
        #elimitate the ;; case
        self.original = [p for p in os.environ["PATH"].split(";") if p!=""]

        #list
        self.elb = gizmos.EditableListBox(self, -1, "System Paths", size=(640,300))
        self.elb.SetStrings(self.original)
        
        #menu
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        menu.Append(ID_SAVE, "&Save")
        menu.Append(ID_RESTORE, "&Restore")
        menu.AppendSeparator()
        menu.Append(ID_EXIT, "&Exit")
        self.Bind(wx.EVT_MENU, self.onSave, id=ID_SAVE)
        self.Bind(wx.EVT_MENU, self.onRestore, id=ID_RESTORE)
        self.Bind(wx.EVT_MENU, self.onExit, id=ID_EXIT)
        menuBar.Append(menu, "&Actions")
        self.SetMenuBar(menuBar)
        
        self.Show(True)
    
    def onExit(self, evt):
        self.Close()
    
    def onSave(self, evt):
        #CHECK CONSISTENCY
        paths = self.elb.GetStrings()
        for p in paths:
            if not os.path.exists(p):
                wx.MessageBox("Warning, The path %s does not exists"%p,
                          "Incoherence detected", wx.ICON_WARNING | wx.OK)
        savePaths = ""
        for p in paths:
            savePaths.join((p,";"))
        print savePaths
        os.putenv("PATH", savePaths)
    
    def onRestore(self, evt):
        self.elb.SetStrings(self.original)
    
if __name__=="__main__":
    app = wx.App()
    app.RestoreStdio()
    SysPathEditor(None)
    
    app.MainLoop()